package my.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.bean.Order;
import my.bean.Tea;
import my.bean.User;
import my.dao.ICartDao;
import my.dao.IOrderDao;
import my.dao.ITeaDao;
import my.dao.IUserDao;
import my.dao.lmpl.ICartDaoImpl;
import my.dao.lmpl.IOrderDaoImpl;
import my.dao.lmpl.ITeaDaoImpl;
import my.dao.lmpl.IUserDaoImpl;
import my.dbUtil.DBUtils;
import my.service.IUserService;

public class IUserServiceImpl implements IUserService {

	@Override
	public int register(User user) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IUserDao userDao = new IUserDaoImpl(conn);
			record = userDao.addUser(user);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.err.println("注册失败,尝试进行事务回滚");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e3) {
				e3.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

	}

	@Override
	public int deleteUserById(int id) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IUserDao userDao = new IUserDaoImpl(conn);
			record = userDao.deleteUserById(id);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败,尝试进行事务回滚");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e3) {
				e3.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyBaseMessage(User user) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IUserDao userDao = new IUserDaoImpl(conn);
			record = userDao.modifyBaseMessage(user);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败,尝试进行事务回滚");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e3) {
				e3.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public void buy(int cid, Order order, Tea tea) throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ICartDao cartDao = new ICartDaoImpl(conn);
			IOrderDao orderDao = new IOrderDaoImpl(conn);
			ITeaDao teaDao = new ITeaDaoImpl(conn);

			cartDao.deleteCart(cid);
			orderDao.addOrder(order);
			teaDao.modifyTeaMessage(tea);

		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyPasswordById(int id, String newPassword) throws SQLException {
		Connection conn = null;
		int record = 0;

		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			IUserDao userDao = new IUserDaoImpl(conn);
			record = userDao.modifyPasswordById(id, newPassword);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败,尝试进行事务回滚");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e3) {
				e3.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<User> findAll() {
		List<User> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IUserDao userDao = new IUserDaoImpl(conn);
			list = userDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public User findByAccount(String account) {
		User user = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IUserDao userDao = new IUserDaoImpl(conn);
			user = userDao.findByAccount(account);
			return user;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public User findByUserId(int id) {
		User user = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IUserDao userDao = new IUserDaoImpl(conn);
			user = userDao.findByUserId(id);
			return user;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public User login(String account, String password) {
		User user = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			IUserDao userDao = new IUserDaoImpl(conn);
			user = userDao.findByUserAccountAndPassword(account, password);
			return user;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

}
